package com.scisoft.scan.control;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;

/**
 * ViewHolder集合类
 * 需要传入HorizontalScrollView的id，作为左滑控制
 */
public class CommonViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;
    private View mConvertView;
    public HorizontalScrollView horizontalScrollView; //用HorizontalScrollView实现的左滑菜单
    public View mShowView;//显示的内容
    public View mHideView;//隐藏的内容

    //构造函数
    public CommonViewHolder(View itemView, int horizontalScrollViewId,int showId,int hideId) {
        super(itemView);
        horizontalScrollView = itemView.findViewById(horizontalScrollViewId);
        mShowView = itemView.findViewById(showId);
        mHideView = itemView.findViewById(hideId);
        mConvertView = itemView;
        mViews = new SparseArray<>();
    }
    public static CommonViewHolder getInstance(Context context, int layoutId, ViewGroup parent,int horizontalScrollViewId,int showId,int hideId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        CommonViewHolder holder = new CommonViewHolder(itemView,horizontalScrollViewId,showId,hideId);
        return holder;
    }

    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }
}
